package com.source.hotelbath.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.ffcs.network.connect.CustomerError;
import com.ffcs.network.connect.ResponseListener;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.source.hotelbath.R;
import com.source.hotelbath.base.BaseFragment;
import com.source.hotelbath.model.OrderListBean;
import com.source.hotelbath.net.CustomerRequestConstants;
import com.source.hotelbath.net.request.OrderListRequest;
import com.source.hotelbath.net.response.HotelListResponse;
import com.source.hotelbath.net.response.OrderListResponse;
import com.source.hotelbath.utils.DialogCallback;
import com.source.hotelbath.weight.listview.ptr.PtrMaterialHeader;
import com.source.hotelbath.weight.listview.ptr.ptrloadmore.PtrLoadMoreBase;
import com.source.hotelbath.weight.listview.ptr.ptrloadmore.PtrLoadMoreListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZhouMeng on 2017/9/25.
 */

public class UndoneFragment extends BaseFragment implements PtrLoadMoreBase.LoadingListener{

    public static final String TAG = UndoneFragment.class.getSimpleName();
    private PtrLoadMoreListView ptlv_content;
    private List<OrderListBean> orderList=new ArrayList<>();
    private DoneItemAdapter adapter;
    private LocalBroadcastManager localBroadcastManager;

    private int page;
    private int hotelId;
    private String postUrl;

    @Override
    protected View createView(LayoutInflater layoutInflater, ViewGroup var2, Bundle var3) {
        View view=layoutInflater.inflate(R.layout.fragment_undone, null);
        findViews(view);
        return view;
    }

    private void findViews(View view) {
        localBroadcastManager = LocalBroadcastManager.getInstance(getContext());
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(CustomerRequestConstants.REFRESH_ORDER);
        localBroadcastManager.registerReceiver(mReceiver,myIntentFilter);
        ptlv_content = (PtrLoadMoreListView) view.findViewById(R.id.ptlv_content);
        ptlv_content.setLastUpdateKey(TAG);
        ptlv_content.getRefreshView().setVerticalScrollBarEnabled(false);
        ptlv_content.registListener(this);
        ptlv_content.disableWhenHorizontalMove(true);

        ptlv_content.setHeader(new PtrMaterialHeader(getActivity()));
        ptlv_content.getPtrFrame().setPinContent(true);
        adapter = new DoneItemAdapter(getActivity(),orderList);
        ptlv_content.getRefreshView().setAdapter(adapter); //这个adapter不用管是为了让自个自定义listview显示
        ptlv_content.setPageSize(15);
        ptlv_content.loadSuccess();

    }

    @Override
    protected void afterCreate() {
        super.afterCreate();
        //网络请求 这个界面需要返回酒店的列表
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                ptlv_content.autoRefresh();
            }
        },300);
    }


    @Override
    public void refresh(int pageIndex, int pageSize) {
        page=0;
        if (hotelId==0){
            postUrl = CustomerRequestConstants.newUrl+CustomerRequestConstants.ORDER_LIST_COMMOND+"05"+"&page="+page;
        }else {
            postUrl=CustomerRequestConstants.newUrl+CustomerRequestConstants.ORDER_LIST_COMMOND+"05"+"&page="+page+"&hotelId="+hotelId;
        }
        Log.d("postUrl",postUrl);
        OkGo.<OrderListResponse>get(postUrl)//
                .tag(this)//
                .execute(new DialogCallback<OrderListResponse>(getActivity(),false) {
                    @Override
                    public void onSuccess(Response<OrderListResponse> response) {
                        OrderListResponse  orderListResponse=response.body();
                        if (orderListResponse!=null&&orderListResponse.getOrderList().size()>0){
                            orderList.clear();
                            orderList.addAll(orderListResponse.getOrderList());
                            adapter.notifyDataSetChanged();
                            if (orderListResponse.getTotalPages()>(page+1)){
                                ptlv_content.setHasNext(true);
                            }else {
                                ptlv_content.setHasNext(false);
                            }
                            ptlv_content.loadSuccess();
                        }else {
                            orderList.clear();
                            adapter.notifyDataSetChanged();
                            ptlv_content.loadSuccess();
                            ptlv_content.showEmptyView();
                        }
//

                    }

                    @Override
                    public void onError(Response<OrderListResponse> response) {
                        super.onError(response);
                        ptlv_content.loadFailure();
                        ptlv_content.showEmptyView();
                        Toast.makeText(getContext(),"请求网络失败",Toast.LENGTH_SHORT).show();
                    }

                });
    }

    @Override
    public void loadMore(int pageIndex, int pageSize) {

        page+=1;
        if (hotelId==0){
            postUrl = CustomerRequestConstants.newUrl+CustomerRequestConstants.ORDER_LIST_COMMOND+"05"+"&page="+page;
        }else {
            postUrl=CustomerRequestConstants.newUrl+CustomerRequestConstants.ORDER_LIST_COMMOND+"05"+"&page="+page+"&hotelId="+hotelId;
        }
        OkGo.<OrderListResponse>get(postUrl)//
                .tag(this)//
                .execute(new DialogCallback<OrderListResponse>(getActivity(),false) {
                    @Override
                    public void onSuccess(Response<OrderListResponse> response) {
                        OrderListResponse  orderListResponse=response.body();
                        if (orderListResponse!=null){
//                            orderList.clear();
                            orderList.addAll(orderListResponse.getOrderList());
                            adapter.notifyDataSetChanged();
                            if (orderListResponse.getTotalPages()>(page+1)){
                                ptlv_content.setHasNext(true);
                            }else {
                                ptlv_content.setHasNext(false);
                            }
                        }
                        ptlv_content.loadSuccess();

                    }

                    @Override
                    public void onError(Response<OrderListResponse> response) {
                        super.onError(response);
                        ptlv_content.loadSuccess();
                        Toast.makeText(getContext(),"请求网络失败",Toast.LENGTH_SHORT).show();
                    }

                });

    }

    private BroadcastReceiver mReceiver  = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (CustomerRequestConstants.REFRESH_ORDER.equals(intent.getAction())){
                //刷新数据
                hotelId=intent.getIntExtra("hotelId",0);
                afterCreate();


            }
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        localBroadcastManager.unregisterReceiver(mReceiver);
    }
}
